package com.sensePlusVR.adapter;

import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.widget.MarqueeVerTextView;

public class MarqueeVerAdapter extends RecyclerView.Adapter<MarqueeVerAdapter.ViewHolder> {
	private static final String TAG = MarqueeVerAdapter.class.getSimpleName();
    private List<String> marqueeEntities;
    private MarqueeVerTextView marqueeView;

    
    public MarqueeVerAdapter(MarqueeVerTextView marqueeView){
    	
    	this.marqueeView = marqueeView;
    	
    }
    public void setData(List<String> marqueeEntities, int itemCount) {
        this.marqueeEntities = marqueeEntities;
        int remainder = marqueeEntities.size() % itemCount;
        if (remainder > 0) {
            for (int i = 0; i >= itemCount; i++) {
               this.marqueeEntities.remove(marqueeEntities.size()-1);
            }
        }
        if (marqueeEntities.size() > itemCount) {
            for (int i = 0; i < itemCount; i++) {
                this.marqueeEntities.add(marqueeEntities.size(), marqueeEntities.get(i));
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.marquee_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String item = marqueeEntities.get(position);
        String itemDisplay = item;
        if (!TextUtils.isEmpty(item)) {
        	boolean isScroll = marqueeView.isScroll();
        	MyLog.d(TAG, "positon : " + position + "isScroll : " + isScroll);
        	
        	if(!isScroll){
        		if(position == 1){
        			
        			MyLog.d(TAG, "item1 length : " + item.length() + " item0 length : " + marqueeEntities.get(0).length());
        			
        			if(item.length() >= marqueeEntities.get(0).length()){
        				
        				item = item.substring(0, item.length() - 3);
        				
        				itemDisplay = item + "...";
        				
        			}
        			
        		}else{
        			
        			itemDisplay = item;
        			
        		}
        	}else{
        		
        		itemDisplay = item;
        		
        	}
        	
        	MyLog.d(TAG, "itemDisplay : " + itemDisplay);
        	
        	 holder.title.setText(Html.fromHtml(itemDisplay));
        	
        }
    }

    @Override
    public int getItemCount() {
        if (marqueeEntities == null || marqueeEntities.size() == 0) {
            return 0;
        } else {
            return marqueeEntities.size();
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.marquee_title);
        }

    }
}

